import Vue from "vue"
import Vuex from "vuex"
Vue.use(Vuex)
const store=new Vuex.Store({
    state:{
        user:[],
        ShopCartGood:[],
        create:0,
        moneyAlreadyCalc:0
    },
    getters:{
        getMoneyAlreadyCalc(state){
            return state.moneyAlreadyCalc;
        },
        getUser(state){
            return state.user;
        },
        getShopCartGood(state){
            return state.ShopCartGood;
        },
        getIsCreate(state){
            return state.create;
        },
        getHasLoginStatus(state){
            for(let item of state.user){
                if(item.isLogin){
                    return true;
                }
            }
            return false;
        }
    },
    mutations:{
        editMoneyAlreadyCalc(state){
            state.moneyAlreadyCalc++;;
        },
        addIsCreate(state,id){
            if(id<=1){
                state.create=id;
            }else{
                state.create++;
            }
        },
        getUserList(state,user){
            state.user.push(...user);
        },
        addUser(state,user){
            let orginStatus=null;
            for(let i=0;i<state.user.length;i++){
                if(state.user[i].id==user.id){
                    if(state.user[i]['userStatus']==user['userStatus']){
                        state.user.splice(i,1,user);
                    }else{
                        user['userStatus']='deny';
                        state.user.splice(i,1,user);
                    }
                    return;
                }
            }
            state.user.push(user);
        },
        changeLoginStatus(state,id=""){
            for(let value of state.user){
                if(value.id==id) Vue.set(value,"isLogin",true);
                else Vue.set(value,"isLogin",false);
            } 
        },
        addShopCard(state,goods){
            for(let i=0;i<state.ShopCartGood.length;i++){
                if(state.ShopCartGood[i].ID==goods.ID){
                    state.ShopCartGood.splice(i,1,goods);
                    return;
                }
            }
            // for(let item of state.ShopCartGood){
            //     if(item.ID==goods.ID){
            //         item.addShopCardNum=goods.addShopCardNum;
            //         return;
            //     }
            // }
            state.ShopCartGood.push(goods);        
        },
        changeShopCardGoodNum(state,goods){
            let index=state.ShopCartGood.indexOf(goods);
            if(index != -1){
                state.ShopCartGood.splice(index,1,goods);
            }else{
                state.ShopCartGood.push(goods);
            }
        },
        delShopCardGood(state,index){
            state.ShopCartGood.splice(index,1);
            let curUserId=null;
            for(let userItem of state.user){
                if(userItem['isLogin']){
                    curUserId=userItem['id'];
                    break;
                }
            }
        
            let localNewShopCart=JSON.parse(localStorage.getItem('newShopCart'));
            localNewShopCart.forEach((newShopCart,index) => {
                if(newShopCart['userId']==curUserId){
                    localNewShopCart[index]['shopCard']=this.getters.getShopCartGood;
                }                
            });
            localStorage.setItem('newShopCart',JSON.stringify(localNewShopCart))
        },
        deleterUserReceiptAddress(state,userId){
            for(let userItem of state.user){
                if(userItem['id']==userId){
                    userItem['realName']="";
                    userItem['tel']="";
                    userItem['streetName']="";
                    userItem['province']="";
                    userItem['city']="";
                    userItem['county']="";
                    return true;  
                }
            }
            return false;
        },
        modifyUserReceiptAddress(state,userInfo){
            for(let userItem of state.user){
                if(userItem['id']==userInfo.id){
                    userItem['realName']=userInfo.realName;
                    userItem['tel']=userInfo.tel;
                    userItem['streetName']=userInfo.streetName;
                    userItem['province']=userInfo.province;
                    userItem['city']=userInfo.city;
                    userItem['county']=userInfo.county;
                    return true;  
                }
            }
            return false;
        },
        addUserOrder(state,UserOrderDate){
            if(localStorage.getItem('newUserOrder')!=null)
            {
                let readyUserOrderDate=JSON.parse(localStorage.getItem('newUserOrder'));
                readyUserOrderDate.push(UserOrderDate);
                localStorage.setItem('newUserOrder',JSON.stringify(readyUserOrderDate));
            }else{
                let readyUserOrderDate=[];
                readyUserOrderDate.push(UserOrderDate);
                localStorage.setItem('newUserOrder',JSON.stringify(readyUserOrderDate));
            }

            if(UserOrderDate.payStatus){
                state.ShopCartGood.splice(0,state.ShopCartGood.length);
            }
            for(let item of state.user){
                if(item.id==UserOrderDate.userId && UserOrderDate.payStatus){
                    item.accountMoney-=UserOrderDate.goodsSum;
                    item.accountMoney=Number.parseFloat(item.accountMoney.toFixed(2));
                    item.userOrder.push(UserOrderDate);
                    return true;
                }
            }
            return false;
        },
        clearShopCart(state){
            state.ShopCartGood.splice(0,state.ShopCartGood.length);
        },
        changeSelectedWhichOne(state,userId=""){
            for(let i=0;i<state.user.length;i++){
                if(state.user[i].id==userId){
                   Vue.set(state.user[i],'selected',true);
                }else{
                   Vue.set(state.user[i],'selected',false);
                }
            }
          
        },
        changeSelectedUserStatus(state,newInfo){
            for(let i=0;i<state.user.length;i++){
                if(state.user[i].id==newInfo.userId){
                   Vue.set(state.user[i],'userStatus',newInfo.newStatus);
                   return;
                }
            }
        },
        addNewUserStatus(state,userInfo){
            for(let item of state.user){
                if(item.id==userInfo.id){
                    Vue.set(item,'userStatus',userInfo.userStatus);
                }
            }
        },
        changeAccountMoney(state,accountMoney){
            for(let item of state.user){
                if(item.isLogin){
                    let money=Number.parseFloat((item.accountMoney-accountMoney).toFixed(2));
                    Vue.set(item,"accountMoney",money);
                }
            }
        },           
    },
    actions:{
        awaitChangeAccountMoney(context,money){
            setTimeout(()=>{
                context.commit("changeAccountMoney",money)
            },10)    
        }
    }
})
export default store;